package com.itianyu.blog.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.itianyu.blog.dao.interfaces.EntryInterface;
import com.itianyu.blog.util.SimpleBloggerConfig;

/**
 * RSS订阅控制层
 * @author wumgji
 */
@Controller
@RequestMapping("/feed")
public class NewsFeedController {

	@Autowired
	private EntryInterface entryInterface;

	@Autowired
	private SimpleBloggerConfig simpleBloggerConfig;

	// 获取RSS订阅信息的默认方法
	@RequestMapping(value = "/news.rss", method = RequestMethod.GET)
	public ModelAndView buildRSS() throws Exception {

		ModelMap model = new ModelMap();

		// RSS订阅的元数据
		model.addAttribute("feedTitle", simpleBloggerConfig.getBlogTitle());
		model.addAttribute("feedDesc", simpleBloggerConfig.getRssFeedDescription());
		model.addAttribute("feedLink", simpleBloggerConfig.getBlogUrl());
		model.addAttribute("feedItemsBaseURI", simpleBloggerConfig.getBlogUrl() + "/app/entry/");

		// RSS订阅的内容
		model.addAttribute("entryItems", entryInterface.findEntryEntities(1, 10, true));

		// 将rssNewsFeedView对应到web-mvc-config.xml
		return new ModelAndView("rssNewsFeedView", model);
	}

}
